/**
 * Mock数据生成器
 * 根据接口文档定义的结构自动生成符合要求的Mock数据
 */

// 生成随机中文姓名
function generateChineseName() {
  const familyNames = ["赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈", "褚", "卫", "蒋", "沈", "韩", "杨"];
  const givenNames = ["伟", "芳", "娜", "敏", "静", "丽", "强", "磊", "军", "洋", "勇", "艳", "杰", "娟", "涛", "明", "超", "秀英", "霞", "平"];
  const familyName = familyNames[Math.floor(Math.random() * familyNames.length)];
  const givenNameLength = Math.random() > 0.5 ? 1 : 2;
  let givenName = "";
  for (let i = 0; i < givenNameLength; i++) {
    givenName += givenNames[Math.floor(Math.random() * givenNames.length)];
  }
  return familyName + givenName;
}

// 生成指定范围的随机数
function generateRandomNumber(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

// 生成随机字符串
function generateRandomString(length = 10) {
  const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  let result = '';
  for (let i = 0; i < length; i++) {
    result += characters.charAt(Math.floor(Math.random() * characters.length));
  }
  return result;
}

// 生成布尔值
function generateRandomBoolean() {
  return Math.random() > 0.5;
}

// 根据字段类型生成对应的数据
function generateDataByType(type) {
  switch (type) {
    case 'string':
      // 特殊处理姓名字段
      if (Math.random() > 0.7) {
        return generateChineseName();
      }
      return generateRandomString(generateRandomNumber(5, 15));
    case 'number':
      // 特殊处理年龄字段
      if (Math.random() > 0.7) {
        return generateRandomNumber(18, 30);
      }
      // 特殊处理ID字段
      if (Math.random() > 0.5) {
        return generateRandomNumber(1, 10000);
      }
      return generateRandomNumber(1, 100);
    case 'boolean':
      return generateRandomBoolean();
    case 'array':
      const arr = [];
      const arrLength = generateRandomNumber(1, 5);
      for (let i = 0; i < arrLength; i++) {
        arr.push(generateRandomString());
      }
      return arr;
    case 'object':
      return {
        key: generateRandomString(),
        value: generateRandomString()
      };
    default:
      return generateRandomString();
  }
}

/**
 * 根据接口文档定义生成Mock数据
 * @param {Object} apiDoc - 接口文档对象
 * @returns {Object} 生成的Mock数据
 */
export function generateMockData(apiDoc) {
  const mockData = {};
  
  // 处理返回体定义
  if (apiDoc.response && Array.isArray(apiDoc.response)) {
    apiDoc.response.forEach(field => {
      if (field.name) {
        // 如果是特殊字段，使用特殊生成规则
        if (field.name.includes('name') || field.name.includes('姓名')) {
          mockData[field.name] = generateChineseName();
        } else if (field.name.includes('age') || field.name.includes('年龄')) {
          mockData[field.name] = generateRandomNumber(18, 30);
        } else if (field.name.includes('id') || field.name.includes('ID')) {
          mockData[field.name] = generateRandomNumber(1, 10000);
        } else {
          mockData[field.name] = generateDataByType(field.type);
        }
      }
    });
  }
  
  return mockData;
}

/**
 * 根据参数定义生成示例参数
 * @param {Object} apiDoc - 接口文档对象
 * @returns {Object} 生成的示例参数
 */
export function generateMockParams(apiDoc) {
  const mockParams = {};
  
  // 处理参数定义
  if (apiDoc.parameters && Array.isArray(apiDoc.parameters)) {
    apiDoc.parameters.forEach(param => {
      if (param.name) {
        // 如果是特殊字段，使用特殊生成规则
        if (param.name.includes('name') || param.name.includes('姓名')) {
          mockParams[param.name] = generateChineseName();
        } else if (param.name.includes('age') || param.name.includes('年龄')) {
          mockParams[param.name] = generateRandomNumber(18, 30);
        } else if (param.name.includes('id') || param.name.includes('ID')) {
          mockParams[param.name] = generateRandomNumber(1, 10000);
        } else {
          mockParams[param.name] = generateDataByType(param.type);
        }
      }
    });
  }
  
  return mockParams;
}

export default {
  generateMockData,
  generateMockParams
};